package pro.lepers.portfolio.view.contents.portfolio.content {
	import fr.digitas.flowearth.ui.text.StyledLabel;

	import pro.lepers.portfolio.view.contents.portfolio.ProjectItem;
	import pro.lepers.portfolio.view.contents.portfolio.data.contents.AbstractContentData;
	import pro.lepers.portfolio.view.contents.portfolio.data.contents.TextContentData;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;

	/**
	 * @author Pierre Lepers
	 */
	public class TextContent extends ThumbContent {
		
		public function TextContent(pcData : AbstractContentData) {
			super( pcData );
			
			_build();
		}

		
		override public function dispose () : void {
			_copy.bitmapData.dispose();
			super.dispose( );
		}

		private function _build () : void {
			
			_container = new Sprite();
			addChild( _container );
			
			_title = new StyledLabel();
			_title.styleName = ".project_tpl_text_title";
			_title.tf.width = 496;
			
			_desc = new StyledLabel();
			_desc.styleName = ".project_tpl_text_desc";
			_desc.tf.width = 496;
			
			_title.text = textData.title;
			_desc.text = textData.desc;
			
			_desc.x = _title.x = 8;
			_title.y = 4;
			_desc.y = _title.y + _title.height + 14;
			
			_title.filters = [ new DropShadowFilter( 1, 45, 0, .2, 4, 4, .8, 3 ) ];
			_desc.filters = [ new DropShadowFilter( 1, 45, 0, .2, 4, 4, .8, 3 ) ];
			
			_container.addChild( _title );
			_container.addChild( _desc );
			
			_copy = new Bitmap();
			_copy.bitmapData = new BitmapData( ProjectItem.THW, ProjectItem.THH, true, 0 );
			_copy.bitmapData.draw( _container );
		}
		
		
		override public function setSize (w : int, h : int) : void {
			super.setSize( w, h );
			
			var needCache : Boolean = ( w != ProjectItem.THW && h != ProjectItem.THH );
			
			if( needCache != _cached ) {
				if( needCache ) {
					removeChild( _container );
					addChild( _copy );
				} else {
					removeChild( _copy );
					addChild( _container );
				}
			}
			
			if( needCache ) {
				_copy.width = w;
				_copy.height = h;
			}
			
			_cached = needCache;
		}

		
		override public function set highQuality (highQuality : Boolean) : void {
			super.highQuality = highQuality;
			
			_copy.smoothing = highQuality;
		}

		
		private var _cached : Boolean = false;
		
		private var _container : Sprite;
		
		private var _copy : Bitmap;
		
		private var _title : StyledLabel;
		private var _desc : StyledLabel;

		
		private function get textData() : TextContentData {
			return _contentData as TextContentData;
		}
		
	}
}
